'use strict';
const Controller = require('egg').Controller;
const fs = require('fs')
const path = require("path")
const svgCaptcha = require("svg-captcha"); //导入验证码文件
class DzfController extends Controller {
    // 作品动态
    async dzfzuoping() {
            // console.log(1111111);
            const { ctx } = this;
            let res = await this.ctx.service.dzf.dzfzuoping();
            // console.log(res, "作品");
            ctx.body = res;
        }
        // 模态弹窗
    async motai() {
        // console.log(1111111);
        const { ctx } = this;
        let opus = this.ctx.request.query.opus;
        let res = await this.ctx.service.dzf.motai(opus);
        // console.log(res, "作品");
        ctx.body = res;
    }

    // 加载更多
    async more() {
        const { ctx } = this;
        let r1 = this.ctx.request.query;
        let res = await this.ctx.service.dzf.more(r1.start);
        // console.log(res);
        ctx.body = res;
    }

    // 点赞作品
    async addZan() {
        // 判断用户是否登录，或者登录信息是否过期
        // console.log(this.ctx.session.id, 8888888888888888);
        if (!this.ctx.session.id) {
            this.ctx.body = {
                info: "未登录或者过期,缓存过期,重新登录",
                code: 4005
            }
        } else {
            let userid = this.ctx.session.id
            let opusid = this.ctx.request.query.opusid
                // console.log(opusid, 999999999999999);
            let res1 = await this.ctx.service.dzf.addZan(opusid, userid)
                // console.log(res1, "判断是否点赞");
            if (!res1[0]) {
                // console.log(88888);
                this.ctx.body = { info: "还没有点赞", code: 4008 }
            } else {
                this.ctx.body = { info: "你已经点赞了呢", code: 2008 }
            }
        }
    }
    async addZan1() {
        let userid = this.ctx.session.id
        let opusid = this.ctx.request.query.opusid
        let res1 = await this.ctx.service.dzf.addZan1(opusid, userid)
        this.ctx.body = res1
            // this.ctx.body = { info: "点赞成功", code: "2000" };
    }


    // 关注作者

    async follow() {
        let star = this.ctx.request.query.star;
        let fans = this.ctx.session.id;
        let res = await this.ctx.service.dzf.follow(fans, star);
        // console.log(res);
        if (!res[0]) {
            console.log("没有关注");
            this.ctx.body = { info: "没有关注", code: 4010 }
        } else {
            console.log("关注");
            this.ctx.body = { info: "已关注", code: 2010 }
        }
    }
    async follow1() {
        let star = this.ctx.request.query.star;
        let fans = this.ctx.session.id;
        let res1 = await this.ctx.service.dzf.follow1(fans, star);
        this.ctx.body = res1
    }

    async comments() {
            const { ctx } = this;
            let r1 = this.ctx.request.query;
            let res = await this.ctx.service.dzf.comments(r1.opusid);
            ctx.body = res;
        }
        // 获取点赞人的头像
    async dianzan() {
            const { ctx } = this;
            let r1 = this.ctx.request.query;
            let res = await this.ctx.service.dzf.dianzan(r1.opusid);
            // console.log(res);
            ctx.body = res;
        }
        // 点击更多获取更多点赞量
    async moredianzan() {
        const { ctx } = this;
        let length = this.ctx.request.query.length;
        let opusid = this.ctx.request.query.opusid;
        // console.log(length, opusid, 22136666666666);
        let res = await this.ctx.service.dzf.moredianzan(opusid, length);
        ctx.body = res;
    }

    async qitawork() {
        const { ctx } = this;
        let r1 = this.ctx.request.query;
        let res = await this.ctx.service.dzf.qitawork(r1.userid);
        ctx.body = res;
    }

    async qitacaipu() {
        const { ctx } = this;
        let r1 = this.ctx.request.query;
        let res = await this.ctx.service.dzf.qitacaipu(r1.userid);
        // console.log(res, 5555555555555);
        ctx.body = res;
    }

    async guanzhu() {
        const { ctx } = this;
        let r1 = this.ctx.request.query;
        let res = await this.ctx.service.dzf.guanzhu(r1.userid);
        ctx.body = res;
    }
    async verify() {
            // 生成验证码
            const verifyObj = svgCaptcha.create({
                size: 4,
                fontSize: 50,
                width: 100,
                height: 40,
                background: '#cc9966',
            });
            // 缓存验证码
            this.ctx.session.verify = verifyObj.text; //缓存验证码中的文字
            // console.log(verifyObj.text);
            this.ctx.body = verifyObj.data; //绘制svg标签
        }
        // 注册
    async register() {
            // 1、判断验证码是否正确
            ///1.1 接受前端传来的数据
            // 1.2 判断验证码不分大小写，是否错误，错误返回一个code码给前端
            let res = this.ctx.request.body;
            // console.log(res);
            if (res.verify.toUpperCase() != this.ctx.session.verify.toUpperCase()) {
                this.ctx.body = { info: "验证码错误", code: 4001 }
            } else {
                // 2、判断用户是否已经注册
                // 2.1 搜索数据库中是否有用户信息(手机号码)
                var sql = `select * from user where email="${res.email}"`; //判断前端输入的邮箱是否存在
                // console.log(sql, 2222);
                var res1 = await this.app.mysql.query(sql);
                if (res1[0]) {
                    this.ctx.body = { info: "用户已注册", code: 4002 }
                } else {
                    // 没注册，就把用户输入的邮箱等信息添加到数据库
                    // 上传头像文件
                    var oldpath = this.ctx.request.files[0].filepath;
                    var newpath = __dirname + "/../public/upload/" + path.basename(oldpath);
                    fs.copyFile(oldpath, newpath, (err) => {
                        // console.log(err);
                        fs.unlink(oldpath, (err) => {})
                    })

                    res.touxiang = "/public/upload/" + path.basename(oldpath) //    头像的新路径
                        //然后把所有数据存到数据库中
                    var sql2 = `insert into user (username,pwd,touxiang,email) values ("${res.user_name}","${res.pwd}","${res.touxiang}","${res.email}")`
                    await this.app.mysql.query(sql2)
                        // console.log(sql2, 99999);
                    this.ctx.body = { info: "注册成功", code: 2001 }
                }
            }

        }
        // 登录
    async login() {
        // 接受前端传来的信息(对象)
        let res = this.ctx.request.body;
        // console.log(res, 12121212212);
        // 判断数据库是否拥有该用户
        let res_sql1 = await this.ctx.service.dzf.ishave(res);
        if (!res_sql1[0]) {
            // 如果不存在 则返回code 码
            this.ctx.body = { info: "未注册，请注册", code: 4003 }
        } else {
            // 判断邮箱和密码是否匹配
            let res_sql2 = await this.ctx.service.dzf.login(res);
            if (!res_sql2[0]) {
                this.ctx.body = { info: "密码错误", code: 4004 }

            } else {
                // 缓存用户id
                this.ctx.session.id = res_sql2[0].id;
                // console.log(res_sql2[0], this.ctx.session.id, 7777777);

                this.ctx.body = { info: "登录成功", code: 2003 }
            }
        }

    }
}
module.exports = DzfController;